package com.zx.mes.advice;

import com.zx.mes.response.BaseResponse;
import com.zx.mes.constant.Constants;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * controller advice 全局异常捕获
 * 例如:
 * "status": 500,
 * "message": "Error updating database.
 * Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
 * check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id=1' at line 1\r\n###
 * The error may involve com.zx.mes.hyl.mapper.AdminMapper.updateById-Inline\r\n### The error occurred while setting parameters\r\n###
 * SQL: UPDATE admin    WHERE id=?\r\n### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
 * You have an error in your SQL syntax;
 * check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id=1' at line 1\n; bad SQL grammar [];
 * nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
 * You have an error in your SQL syntax;
 * check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id=1' at line 1",
 *
 * @author huayunlong
 * @date 2018-8-13
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<BaseResponse> handle(Exception e) {
        e.printStackTrace();
        BaseResponse response = new BaseResponse(Constants.DEFAULT_RESPONSE_STATUS_SERVICE_EXCEPTION, e.getMessage());
        return ResponseEntity.ok(response);

    }
}
